package com.shushan.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.common.ui.UnScrollListView;
import com.shushan.base.Constants;
import com.shushan.pages.bean.TypesExerciseRecordBean;
import com.shushan.shushanbao.R;
import com.shushan.util.ToastUtils;
import com.shushan.util.ToolUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: MyCollectionAndErrListAdapter
 * @Desciption: //收藏列表和错题列表的适配器
 * @author: zhangshihao
 * @date: 2018-04-23
 */
public class MyCollectionAndErrListAdapter extends RecyclerView.Adapter<MyCollectionAndErrListAdapter.ViewHolder> {

    private static final String TAG = MyCollectionAndErrListAdapter.class.getSimpleName();
    private Context context;
    private OnItemClickListener onItemClickListener;

    private List<TypesExerciseRecordBean> typesExerciseRecordBeans;
    private List<HashMap<Integer, Integer>> nums;

    private boolean fromWrongTopic = false;

    public MyCollectionAndErrListAdapter(Context context, List<TypesExerciseRecordBean> typesExerciseRecordBeans, List<HashMap<Integer, Integer>> nums) {
        this.context = context;
        this.typesExerciseRecordBeans = typesExerciseRecordBeans;
        this.nums = nums;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setFromWrongTopic(boolean isFromWrongTopic) {
        this.fromWrongTopic = isFromWrongTopic;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.lv_my_collection_item, null, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TypesExerciseRecordBean bean = typesExerciseRecordBeans.get(position);
        int count = 0;
        int type = bean.type;
        for(int i=0;i<nums.size();i++){
            HashMap<Integer,Integer> map = nums.get(i);
            for(int key : map.keySet()){
                if(key == type){
                    count = map.get(key);
                }
            }
        }
        holder.tvTotalNum.setText(count + "题");
        if (!fromWrongTopic) {
            holder.tvExerciseType.setText(ToolUtils.getTypeNameByTypeId(type) + "收藏");
        } else {
            holder.tvExerciseType.setText(ToolUtils.getTypeNameByTypeId(type) + "错题");
        }

        MyItemListAdapter myItemListAdapter = new MyItemListAdapter(bean, position);
        holder.itemListView.setAdapter(myItemListAdapter);
    }

    @Override
    public int getItemCount() {
        return typesExerciseRecordBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvExerciseType;
        private TextView tvTotalNum;
        private UnScrollListView itemListView;
        private LinearLayout llParentLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            tvExerciseType = itemView.findViewById(R.id.tv_exercise_type);
            tvTotalNum = itemView.findViewById(R.id.tv_topic_total_num);
            itemListView = itemView.findViewById(R.id.lv_my_collection_item);
            llParentLayout = itemView.findViewById(R.id.ll_parent_layout);
        }
    }

    private class MyItemListAdapter extends BaseAdapter {

        private TypesExerciseRecordBean typesExerciseRecordBean;
        private int exercises_type = 0;

        public MyItemListAdapter(TypesExerciseRecordBean typesExerciseRecordBean, int position) {
            this.typesExerciseRecordBean = typesExerciseRecordBean;
            this.exercises_type = position + 1;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (typesExerciseRecordBean == null) {
                return null;
            }

            ItemViewHolder holder = null;
            if (convertView == null) {
                holder = new ItemViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_my_collection_item, null, false);
                holder.ivSubjectLable = convertView.findViewById(R.id.iv_subject_lable);
                holder.tvSubjectExerciseNum = convertView.findViewById(R.id.tv_subject_err_num);
                holder.tvSubjectName = convertView.findViewById(R.id.tv_subject_name);
                convertView.setTag(holder);
            } else {
                holder = (ItemViewHolder) convertView.getTag();
            }
            final int subject_id = position + 7;
            switch (position) {
                case 0:
                    holder.ivSubjectLable.setImageResource(R.drawable.ic_red_chineses);
                    holder.tvSubjectName.setText("语文");
                    break;
                case 1:
                    holder.ivSubjectLable.setImageResource(R.drawable.ic_blue_math);
                    holder.tvSubjectName.setText("数学");
                    break;
                case 2:
                    holder.ivSubjectLable.setImageResource(R.drawable.ic_green_english);
                    holder.tvSubjectName.setText("英语");
                    break;
                default:
                    holder.ivSubjectLable.setImageResource(R.drawable.ic_red_chineses);
                    holder.tvSubjectName.setText("语文");
                    break;
            }

            holder.tvSubjectExerciseNum.setText(typesExerciseRecordBean.err_num_of_subjects[position] + "");
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (typesExerciseRecordBean.err_num_of_subjects[position] <= 0) {
                        if (fromWrongTopic) {
                            ToastUtils.showToast(context, "无错题记录");
                        } else {
                            ToastUtils.showToast(context, "无收藏");
                        }
                    } else {
                        onItemClickListener.onItemClick(typesExerciseRecordBean.err_num_of_subjects[position], subject_id, typesExerciseRecordBean.type);
                    }
                }
            });

            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return typesExerciseRecordBean.err_num_of_subjects[position];
        }

        @Override
        public int getCount() {
            return typesExerciseRecordBean.err_num_of_subjects.length;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        private class ItemViewHolder {
            private ImageView ivSubjectLable;
            private TextView tvSubjectName;
            private TextView tvSubjectExerciseNum;
        }

    }

    public interface OnItemClickListener {
        void onItemClick(int errNum, int subject_id, int exercises_type);
    }

}
